<?php 
namespace Admin\Controller;

class AdminController extends CommonController{
	public function add(){
		if(IS_GET){
			//获取所有的角色
			$role = D('Role')->select();
			$this->assign('role',$role);
			$this->display();
		}else{
			$model = D('Admin');
			$data = $model->create();
			if(!$data){
				$this->error($model->getError());
			}
			$model->add($data);//model调用钩子
			$this->success('写入数据成功');
		}
	}

	//获取数据显示
	public function index(){
		$model = D('Admin');
		$data = $model->listData();
		// dump($data);die;
		$this->assign('data',$data);
		$this->display();
	}

	//删除管理员
	public function dels(){
		$admin_id = intval(I('get.admin_id'));		
		if($admin<=1){
			$this->error('参数错误');
		}
		$res = D('Admin')->remove($admin_id);
		if($res===false){
			$this->error("删除失败");
		}
		$this->success('删除成功');
	}

	//编辑
	public function edit(){
		$model = D('Admin');
		if(IS_GET){
			$admin_id=intval(I("get.admin_id"));
			//获取用户名信息，及角色id 
			$info = $model->findOne($admin_id);
			//获取所有的角色
			$role = D('Role')->select();
			$this->assign("role",$role);
			$this->assign("info",$info);
			$this->display();
		}else{
			$data = $model->create();
			if(!$data){
				$this->error($model->getError());
			}
			if($data['id']<=1){
				$this->error('参数错误');
			}
			$model->update($data);
			$this->success("修改成功",U('index'));
		}
	}
}
